package com.ycMc.service.impl;

import com.ycMc.bean.response.ObjectResponse;
import com.ycMc.common.bean.Response;
import com.ycMc.constant.UserConstant;
import com.ycMc.domain.User;
import com.ycMc.mapper.UserMapper;
import com.ycMc.service.BaseService;
import com.ycMc.service.UserService;
import com.ycMc.utils.BaseUtils;
import com.ycMc.utils.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;

import java.util.List;

/**
 * Created by libo2 on 2017/8/23.
 */
@Service
public class UserServiceImpl extends BaseService implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ObjectResponse checkLogin(String loginName, String password) {

        ObjectResponse response = new ObjectResponse();

        User user = userMapper.checkLogin(loginName,password);

        if(user!=null){
            response.setObject(user);
            response.setStatusCode(Response.SUCCESS);
            response.setMsg("登陆成功");
        }
        else
        {
            response.setStatusCode(Response.ERROR_PASSWORD);
            response.setMsg("登录失败，用户名或密码错误");
        }
        return response;
    }

    @Override
    public void delete(String userId) {
        userMapper.delete(userId);
    }

    @Override
    public void insert(User user) {
        user.setCreateTime(System.currentTimeMillis());
        user.setId(0);
        user.setLoginName(user.getPhone());
        user.setPassword(EncryptUtil.md5("mc" + user.getPhone()));
        user.setUserId(BaseUtils.generateUserId());
        user.setPermission(UserConstant.USER_LEVEL);
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.select(null,null,null,null,0,null,null,0,0);
    }

    @Override
    public User selectOne(String userId) {
        return userMapper.selectOne(userId);
    }

    @Override
    public List<User> select(User user) {
        List<User>  userList = userMapper.select(user.getLoginName(), user.getRealName(), user.getAddress(), user.getPhone(), user.getPermission(), user.getShoppingAddress(),user.getUserId(), 0, 0);
        return userList;
    }

    @Override
    public List<User> query(String keyword) {
        return userMapper.query(keyword,0,0);
    }
}
